package com.anrongtec.zcpt.openapi.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * JSON转换工具库(Json String to Object,Object to Json String,代码由Google提供 google-gson
 * Version 2.2.4)
 * 
 * @author qingf
 */
public class JSONHelper {

	private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();

	public static Gson getGson() {
		return gson;
	}

	public static String toJson(Object obj) {
		try {
			return gson.toJson(obj);
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> T parseInfoFromJson(String json, Class<T> classOfT) {
		try {
			return gson.fromJson(json, classOfT);
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> T parseInfoFromJson(String json, Type type) {
		return gson.fromJson(json, type);
	}

	public static <T> T parseInfoFromJson(JsonElement jsonelement, Class<T> classOfT) {
		try {
			return gson.fromJson(jsonelement, classOfT);
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> Map<String, JsonElement> parseElementMapFromJson(String json) {
		try {
			JsonElement el = new JsonParser().parse(json);
			if (el.isJsonObject() == false)
				return null;
			Map<String, JsonElement> objs = new Hashtable<String, JsonElement>();
			for (Entry<String, JsonElement> entry : el.getAsJsonObject().entrySet()) {
				objs.put(entry.getKey(), entry.getValue());
			}
			return objs;
		} catch (Exception e) {
			return null;
		}
	}

	public static List<JsonElement> parseListFromJson(String json) {
		try {
			JsonElement el = new JsonParser().parse(json);
			if (el.isJsonObject() == false)
				return null;
			List<JsonElement> objs = new ArrayList<JsonElement>();
			for (Entry<String, JsonElement> entry : el.getAsJsonObject().entrySet()) {
				objs.add(entry.getValue());
			}
			return objs;
		} catch (Exception e) {
			return null;
		}
	}

	public static Hashtable<String, String> parseTableFromJson(String Json) {
		try {
			return gson.fromJson(Json, new TypeToken<Hashtable<String, String>>() {
			}.getType());
		} catch (Exception e) {
			return null;
		}
	}

	public static <T> List<T> parseListFromJson(String json, Class<T> classOfT) {
		try {
			JsonElement el = new JsonParser().parse(json);
			if (el.isJsonArray() == false || el.getAsJsonArray().size() <= 0)
				return null;

			Iterator<?> it = el.getAsJsonArray().iterator();
			List<T> objs = new ArrayList<T>();
			while (it.hasNext()) {
				objs.add(gson.fromJson((JsonElement) it.next(), classOfT));
			}
			return objs;
		} catch (Exception e) {
			return null;
		}
	}

	public static String toJsonSort(Object obj) {
		try {
			Map<Object, Object> map = gson.fromJson(toJson(obj), new TypeToken<TreeMap<Object, Object>>() {
			}.getType());
			return gson.toJson(map);
		} catch (Exception e) {
			return null;
		}
	}

	public static Map<Object, Object> parseMapFromJson(String Json) {
		try {
			return gson.fromJson(Json, new TypeToken<TreeMap<Object, Object>>() {
			}.getType());
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 返回具体的key获取的信息
	 * 
	 * @param key
	 * @param json
	 * @return
	 */
	public static String getStringByKeyFromJson(String key, String json) {
		JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
		return obj.get(key).getAsString();

	}
}